//
//  oldRegex.m
//  laonianbang
//
//  Created by 勇拓 李 on 17/10/9.
//  Copyright © 2017年 zaiyou. All rights reserved.
//

#import "oldRegex.h"
#import <CommonCrypto/CommonDigest.h>

@implementation oldRegex

//验证码
#define SMSCODE_REGEX @"^[0-9]{6}$"

//手机号
#define MOBILE_NUM_PREFIX @"^(13[0-9]|15[0-9]|17[0-9]|18[0-9]|19[0-9]|14[57])"
#define MOBILE_NUM_REGEX MOBILE_NUM_PREFIX"[0-9]{8}$"

//验证是否是合法手机号
+ (BOOL) isMobileNo: (NSString*)text
{
    if (!text) {
        return NO;
    }
    
    /* check phone number */
    
    // remove all spaces
    NSString* trimmedMobileNo = [text stringByReplacingOccurrencesOfString:@"[\\s]+"
                                                                withString:@""
                                                                   options:NSRegularExpressionSearch
                                                                     range:NSMakeRange(0, [text length])];
    
    
    // remove the leading "+86"
    NSString* noCountryCodeMobileNo = [trimmedMobileNo stringByReplacingOccurrencesOfString:@"^\\+86"
                                                                                 withString:@""
                                                                                    options:NSRegularExpressionSearch
                                                                                      range:NSMakeRange(0, [trimmedMobileNo length])];
    
    
    // remove the hyphen in mobile
    NSString* hyphenRemovedMobileNo = [noCountryCodeMobileNo stringByReplacingOccurrencesOfString:@"[\\-]"
                                                                                       withString:@""
                                                                                          options:NSRegularExpressionSearch
                                                                                            range:NSMakeRange(0, [noCountryCodeMobileNo length])];
    
    
    // It is nither an email address nor mobile number
    if ( [hyphenRemovedMobileNo length] != 11 )
        return NO;
    
    return [self matchRegex:MOBILE_NUM_REGEX string:hyphenRemovedMobileNo];
}

+ (BOOL) matchRegex:(NSString*)regexStr string:(NSString*)str
{
    NSError *err = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&err];
    NSUInteger numOfMatch = [regex numberOfMatchesInString:str
                                                   options:NSMatchingAnchored
                                                     range:NSMakeRange(0, [str length])];
    
    return (numOfMatch>0);
    
}


+ (BOOL) isSMSCode:(NSString*)smsCode
{
    if (!smsCode) {
        return NO;
    }
    
    return [self matchRegex:SMSCODE_REGEX string:smsCode];
}


//md5加密
+ (NSString *) md5:(NSString *) input
{
    const char *cStr = [input UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, (unsigned int)strlen(cStr), digest ); // This is the md5 call
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    
    return  output;
}

+ ( BOOL ) isValidateIDNumber : ( NSString * ) idNumber //检查身份证是否合法
{
    if( idNumber.length != 18 ) return NO;
    
    NSString * idNumberRegex = @"(^[0-9]{15}$)|([0-9]{17}([0-9]|X)$)";
    NSPredicate * idNumberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", idNumberRegex];
    return [idNumberTest evaluateWithObject:idNumber];
}

+ ( BOOL ) isValidatePassword : ( NSString * ) password //检查密码是否合法
{
    NSString * passwordRegex = @"^[a-zA-Z0-9]{6,18}+$";
    NSPredicate * passwordTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", passwordRegex];
    return [passwordTest evaluateWithObject:password];
}

+ ( BOOL ) isChineseName : ( NSString * ) name //检查姓名是否合法
{
    NSString * nameRegex = @"^[一-龥]{1,20}";
    NSPredicate * nameTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", nameRegex];
    return [nameTest evaluateWithObject:name];
}

@end
